package tice.Confucius;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Confucius extends Activity {
	
	private static String PREFS_NAME = "tice.Confucius";
	private static final int DIALOG_CONTENTS_LIST = 1;
	static final int[] mStringtable = { 
										R.array.Chapter_1,
										R.array.Chapter_2,
										R.array.Chapter_3,
										R.array.Chapter_4,
										R.array.Chapter_5,
										R.array.Chapter_6,
										R.array.Chapter_7,
										R.array.Chapter_8,
										R.array.Chapter_9,
										R.array.Chapter_10,
										R.array.Chapter_11,
										R.array.Chapter_12,
										R.array.Chapter_13,
										R.array.Chapter_14,
										R.array.Chapter_15,
										R.array.Chapter_16,
										R.array.Chapter_17,
										R.array.Chapter_18,
										R.array.Chapter_19,
										R.array.Chapter_20,
									};
	int mchapter = 0;
	int mParagraph = 1;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView text = (TextView) findViewById(R.id.TextView);
        TextView title = (TextView) findViewById(R.id.titleView);
        TextView vicetitle = (TextView) findViewById(R.id.vicetitleView);
        
        title.setTextSize ((float)30);
        vicetitle.setTextSize ((float)24);
        text.setTextSize ((float)18);
        
        Button btnPrev = (Button) findViewById(R.id.btnPrev);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnContents = (Button) findViewById(R.id.btnContents);
        
        btnPrev.setOnClickListener(mPrevious);
        btnNext.setOnClickListener(mNext);
        btnContents.setOnClickListener(mContents);
        
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
        mchapter = settings.getInt("mchapter", 0); 
        mParagraph = settings.getInt("mParagraph", 1);

    }
 
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_CONTENTS_LIST:
            return new AlertDialog.Builder(Confucius.this)
            .setTitle(R.string.app_name)
            .setItems(R.array.Contents, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                	mchapter = which;
                	mParagraph = 1;
                	UpdateView();
                }
            })
            .create();
        }
        return null;
    }
    
	private OnClickListener mContents = new OnClickListener(){
		@Override
		public void onClick(View v) {
			showDialog(DIALOG_CONTENTS_LIST);
		}
	};
    
	private OnClickListener mNext = new OnClickListener(){
		@Override
		public void onClick(View v) {

			Button btnPrev = (Button) findViewById(R.id.btnPrev);

	        CharSequence[] stringtext = Confucius.this.getResources().getTextArray(mStringtable[mchapter]);

	        btnPrev.setEnabled(true);
			
			mParagraph ++;
			
			if( mParagraph == stringtext.length){
				mchapter ++;
				mParagraph = 1;
			}

			UpdateView();
		}
		
	};

	private OnClickListener mPrevious = new OnClickListener(){
		@Override
		public void onClick(View v) {
			
			Button btnNext = (Button) findViewById(R.id.btnNext);

	        btnNext.setEnabled(true);

			mParagraph --;
			
			if( mParagraph == 0){
				mchapter --;
		        CharSequence[] stringtext = Confucius.this.getResources().getTextArray(mStringtable[mchapter]);
				mParagraph = stringtext.length - 1;
			}
			
			UpdateView();
		}
	};
	
	private void UpdateView(){
		
        CharSequence[] stringtext = this.getResources().getTextArray(mStringtable[mchapter]);
        
        TextView text = (TextView) findViewById(R.id.TextView);
        TextView title = (TextView) findViewById(R.id.titleView);
        TextView vicetitle = (TextView) findViewById(R.id.vicetitleView);

        title.setText(String.format("Chapter %d", (mchapter + 1)));
        vicetitle.setText(String.format("%s", stringtext[0]));
        text.setText(String.format("%d. %s", (mParagraph), stringtext[mParagraph]));
        
		Button btnPrev = (Button) findViewById(R.id.btnPrev);
		Button btnNext = (Button) findViewById(R.id.btnNext);

		btnPrev.setEnabled(true);
		btnNext.setEnabled(true);
		
        if(mchapter == 0 && mParagraph == 1){
        	btnPrev.setEnabled(false);
        }
        
        if(mchapter == mStringtable.length - 1 && mParagraph == stringtext.length -1){
        	btnNext.setEnabled(false);
        }
        

	}

	@Override
	protected void onResume() {
		super.onResume();
		
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
        mchapter = settings.getInt("mchapter", 0); 
        mParagraph = settings.getInt("mParagraph", 1);	
        
        UpdateView();
	}

	@Override
	protected void onStop() {
		super.onStop();
		
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
        SharedPreferences.Editor editor = settings.edit(); 
        editor.putInt("mchapter", mchapter); 
        editor.putInt("mParagraph", mParagraph); 
        editor.commit();  		
	}

}